<template>
    <div class="add">
     <el-card>
      <!-- 步骤 -->
      <el-steps :active="active*1" finish-status="success">
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- tab  headers设置token 成功和失败 on-remove、、 on-error-->
           <el-tabs tab-position="left" style="min-height: 600px" v-model="activeName"  @tab-click="handleClick" :before-leave="beforeleave">
        <el-tab-pane label="商品信息" name="0">
        <!-- 表单 -->
         <el-form :model="allList"  ref="allList" label-width="100px" class="demo-ruleForm">
  <el-form-item label="商品名称" prop="name">
    <el-input v-model="allList.goods_name"></el-input>
  </el-form-item>
  <el-form-item label="商品价格" prop="price">
    <el-input v-model="allList.goods_price" type="number" value="0"></el-input>
  </el-form-item>
   <el-form-item label="商品重量" prop="weight">
    <el-input v-model="allList.goods_weight"></el-input>
  </el-form-item>
   <el-form-item label="商品数量" prop="number">
    <el-input v-model="allList.goods_number"></el-input>
  </el-form-item>
   <el-form-item label="商品分类" prop="goods_cat">
       <el-cascader
              ref="cascader"
              v-model="checkedArr"
              :options="goodslistArr"
              :props="defaultProps"
              @change="handleChange"
            ></el-cascader>
  </el-form-item>
</el-form>
        </el-tab-pane>
        <el-tab-pane label="商品参数" name="1">
         <el-form>
          <el-form-item v-for="(item, index) in manyList" :key="index">
            <div style="margin-top: 20px">
              <el-checkbox-group v-model="item.attr_vals" size="mini">
                <el-checkbox
                  :label="item"
                  border
                  v-for="(item, i) in item.attr_vals"
                  :key="i"
                ></el-checkbox>
              </el-checkbox-group>
            </div>
          </el-form-item>
        </el-form>
        </el-tab-pane>
        <el-tab-pane label="商品属性" name="2">
        <el-form ref="form">
          <el-form-item
            :label="item.attr_name"
            v-for="(item, i) in onlylist"
            :key="i"
          >
            <el-input v-model="item.attr_name"></el-input>
          </el-form-item>
        </el-form>
        </el-tab-pane>
        <el-tab-pane label="商品图片" name="3">
          <el-upload
            class="upload-demo"
            action="http://82.156.30.240:8888/api/private/v1/upload"
            :headers="headers" 
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :on-error="handleError"
            :file-list="fileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>

        </el-tab-pane>
        <el-tab-pane label="商品内容" name="4">
          <!-- 商品内容 -->
          <quill-editor
            v-model="goods_msg"
            :options="editorOption"
          ></quill-editor>
          <el-button  type="primary" @click="goodsOK">添加商品</el-button>
        </el-tab-pane>
      </el-tabs>
      </el-card>
    </div>
</template>

<script>
import { hqgoods, getCategorieslist } from "@/http/goodsadd.js";
import {addgoods} from "@/http/goods.js"
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
export default {
  data() {
    return {
      cateId: "",
      activeName: "",
      onlylist: [],
      manyList: [],
      allList: {
        goods_name: "",
        goods_price: "",
        goods_weight: "",
        goods_number: "",
        goods_cat: ""
      },
      defaultProps: {
        label: "cat_name",
        children: "children",
        value: "cat_id"
      },
      allList: {
        pics: [],
        goods_introduce: "", //内容
        attrs: []
      },
      goodslistArr: [],
      checkedArr: [],
      active: 0,
      goods_msg: "",
      editorOption: {
        modules: {
          toolbar: [
            ["bold", "italic", "underline", "strike"],
            [{ header: 1 }, { header: 2 }],
            [{ list: "ordered" }, { list: "bullet" }],
            [{ script: "sub" }, { script: "super" }],
            [{ indent: "-1" }, { indent: "+1" }],
            [{ direction: "rtl" }],
            [{ size: ["small", false, "large", "huge"] }],
            [{ header: [1, 2, 3, 4, 5, 6, false] }],
            [{ color: [] }, { background: [] }],
            [{ font: [] }],
            [{ align: [] }],
            ["clean"],
            ["link", "image", "video"]
          ]
        }
      },
      fileList: [],
      headers: {
        Authorization: this.$store.getters.token
      },
      queryInfo: {
        pagesize: 5,
        pagenum: 0
      }
    };
  },
  created() {
    this.hqgoods();
  },
  computed: {
    current_cate_id() {
      return this.checkedArr[this.checkedArr.length - 1];
    }
  },
  components: {
    quillEditor
  },
  methods: {
      async handleClick() {
      this.active = this.activeName * 1;
      if (this.activeName == 1) {
        let res = await getCategorieslist(this.current_cate_id);
        console.log(res);
        res.data.forEach(item => {
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(",") : [];
        });

        this.manyList = res.data;
      } else if (this.activeName == 2) {
        let res = await getCategorieslist(this.current_cate_id, "only");
        console.log(res);
        this.onlylist = res.data;
      }
    },
    //添加商品
    async goodsOK() {
      this.allList.goods_cat = this.checkedArr.join(",");
      console.log( this.allList.goods_cat);

      let manyArr = this.manyList.map(item => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals.join(",")
        };
      });

      //处理静态参数
      let onlyArr = this.onlylist.map(item => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals
        };
      });

      //合并动静参数
      let mergeAtrs = [...manyArr, ...onlyArr];
      console.log(mergeAtrs);
      console.log(this.allList);
      this.allList.attrs = mergeAtrs;
      const res = await addgoods(this.allList);
      console.log(res);

      this.$router.push('/goods')
    },
    async hqgoods() {
      let res = await hqgoods(this.queryInfo);
      console.log(res);
      // console.log(res.data.result);
      this.goodslistArr = res.data.result;
      this.goodslistArr.cat_name
      console.log(this.goodslistArr);
    },
    handleChange(val) {
      console.log(val);
      if (val.length != 3) {
        this.$message.error("请选择三级分类");
        this.checkedArr = [];
        return false;
      } else {
      }
    },
    //tabs切换如果基本信息没有写不能往后添加
    beforeleave(newVal, oldVal) {
      if (oldVal === "0" && this.checkedArr.length !== 3) {
        this.$message({
          message: "请先选择商品分类",
          type: "error"
        });

        return false;
      }
    },
    handleSuccess(val) {
       console.log(val)
      this.allList.pics = val.data.tmp_path
      // console.log(response, file, fileList);
    },
    handleError(err, file, fileList) {
      console.log(err, file, fileList);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    }
  }
};
</script>

<style>
</style>
